// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])
    .run(function ($ionicPlatform, $state, $rootScope, $ionicPopup, AppService, Storage) {
        $ionicPlatform.ready(function () {
            // var config = Storage.get("config");
            // var ip = config.ip;
            // var port = config.port;
            // var pjqNum = config.pjqNum;
            // if (ip && port && pjqNum) {//如果服务器配置不为空
            //     $state.go("app.home");
            // }else{
            //     $state.go("app.home");
            // }
        });
        $rootScope.$on('$stateChangeStart', function (evt, toState, toParams, fromState, fromParams) {
            // evt.preventDefault();
            // Storage.remove("config");
            var config = Storage.get("config");
            if (toState.name == "config") {
                console.log(config);
                if (config) {//如果配置不为空
                    $state.go("app.home");
                }
            }

            if (toState.name == "app.home") {
                var needInitHome = $rootScope.needInitHome;
                if (!needInitHome) {//如果没有初始化过
                    if (config) {
                        var pjqNum = config.pjqNum;
                        AppService.init(pjqNum).then(function (res) {
                            console.log(JSON.stringify(res));
                            if (res && res.state == "ok") {
                                //包含用户信息
                                Storage.set("initData", res.data);
                                $rootScope.needInitHome = false;
                                $state.go("app.home")
                            }
                        })
                    }
                }
            }
        });
    })
    .config(function ($stateProvider, $urlRouterProvider) {
        $stateProvider

            .state('app', {
                url: '/app',
                templateUrl: 'templates/app.html',
                controller: 'AppCtrl'
            })
            .state('config', {
                url: '/config',
                cache: false,
                templateUrl: 'templates/config.html',
                controller: 'ConfigCtrl'
            })
            .state('app.home', {
                url: '/home',
                cache: false,
                views: {
                    'mainContent': {
                        templateUrl: 'templates/home.html',
                        controller: 'HomeCtrl'
                    }
                }
            })
            .state('app.eval', {
                url: '/eval',
                cache: false,
                views: {
                    'mainContent': {
                        templateUrl: 'templates/eval.html',
                        controller: 'EvalCtrl'
                    }
                }
            })
            .state('app.call', {
                url: '/call',
                cache: false,
                views: {
                    'mainContent': {
                        templateUrl: 'templates/call.html',
                        controller: 'CallCtrl'
                    }
                }
            })
            .state('app.noticeDetail', {
                url: '/noticeDetail',
                cache: false,
                params:{"id":null,"index":null},
                views: {
                    'mainContent': {
                        templateUrl: 'templates/noticeDetail.html',
                        controller: 'NoticeDetailCtrl'
                    }
                }
            })
        // if none of the above states are matched, use this as the fallback
        $urlRouterProvider.otherwise('/config');
    });
